package com.endava.spring.controller;

import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.endava.spring.beans.Account;
import com.endava.spring.beans.Person;
import com.endava.spring.dao_impl.AccountJDBCTemplate;
import com.endava.spring.dao_impl.PersonJDBCTemplate;

public class MainController {

	public static void main(String[] args) {

		BeanFactory factory = new ClassPathXmlApplicationContext(
				"appContext.xml");

		AccountJDBCTemplate accountTemplate = (AccountJDBCTemplate) factory
				.getBean("accountJDBCTemplate");

		PersonJDBCTemplate personTemplate = (PersonJDBCTemplate) factory
				.getBean("personJDBCTemplate");

		// System.out.println(" > Populating rows ... ");
		// accountTemplate.create(2, "spending_account", 1200012);
		// accountTemplate.create(21, "saving_account", 12222);

		// delete some rows. then pretty print; update
//		accountTemplate.delete(11);
//		accountTemplate.update(2, 990);

		System.out.println(" > List records ... ");
		List<Account> accounts = accountTemplate.getAllAccounts();

		for (Account account : accounts) {
			System.out.println(account.toString());
		}
		
		// make new records in Person table
//		personTemplate.create(2, "john", "williams", "str. of Joly");
//		personTemplate.create(5, "sam", "willson", "str. of Nowbody");
		
		// update & delete operations on Person table
//		personTemplate.update(5, "str. of Happiness");
//		personTemplate.deleteAccount(4);

		System.out.println("\n >> Person table... ");
		List<Person> persons = personTemplate.read();
		
		for (Person person : persons) {
			System.out.println(person.toString());
		}

	}

}
